package pl.prv.polanie.client.testingprototypes;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

public class CrossBrowserHTML extends HTML {
	
	
	boolean attached = false;
	
	public CrossBrowserHTML() {
		super("przykladowy HTML");
		this.setWidth("200px");
		this.setHeight("100px");
//		this.setPixelSize(200, 100);
		this.setStyleName("test");
	}
	
	public void setPixelSize(int width, int height) {
		super.setPixelSize(width, height);
		
		int w = this.getOffsetWidth();
		int h = this.getOffsetHeight();
		
		if (w != width || h != height) {
			width -= w-width; 
			height -= h-height;
			super.setPixelSize(width, height);
		}
	}
	
	public void setOffsetWidth(int offsetWidth) {
		validateAttached();
		
		super.setWidth(offsetWidth+"px");
		int w = this.getOffsetWidth();
		
		if (w != offsetWidth) {
//			Window.alert("musialem jeszcze raz setWidth w FF");	
			offsetWidth -= w-offsetWidth; 
			super.setWidth(offsetWidth+"px");
		}
	}

	public void setOffsetHeight(int offsetHeight) {
		validateAttached();
		
		super.setHeight(offsetHeight+"px");
		int h = this.getOffsetHeight();
		
		if (h != offsetHeight) {
			offsetHeight -= h-offsetHeight; 
			super.setHeight(offsetHeight+"px");
		}
	}
	
    private void validateAttached() {
    	if (!attached) {
            throw new IllegalStateException("Widget must be attached first.");
        }
    }
    
    protected void onAttach() {
    	super.onAttach();
    	attached = true;
    }
}
